﻿//-----------------------------------------------------------------------
// <copyright file="ImageLoaderFrm.cs" company="DylanBox">
// Copyright (c) DylanBox Corporation. All rights reserved.
// </copyright>
// <summary></summary>
// <reference>
/*
 *String to bytes:
 *  byte[] bytes = Encoding.UTF8.GetBytes("foo");
 *Bytes to string:
 *  string foo = Encoding.ASCII.GetString(bytes);* 
 * 
 *But what if you don’t know the encoding type? 
 *String to bytes:
   1:  public static byte[] ConvertStringToBytes(string input)   
 * 2:  {   
 * 3:    MemoryStream stream = new MemoryStream();   
 * 4:       
 * 5:    using (StreamWriter writer = new StreamWriter(stream))   
 * 6:    {   
 * 7:      writer.Write(input);   
 * 8:      writer.Flush();   
 * 9:    }  
 * 10:     
 * 11:    return stream.ToArray();  
 * 12:  }
Bytes to string:
   1:  public static string ConvertBytesToString(byte[] bytes)   
 * 2:  {   
 * 3:    string output = String.Empty;   
 * 4:      
 * 5:    MemoryStream stream = new MemoryStream(bytes);   
 * 6:    stream.Position = 0;   
 * 7:      
 * 8:    using (StreamReader reader = new StreamReader(stream))   
 * 9:    {  
 * 10:      output = reader.ReadToEnd();  
 * 11:   }  
 * 12:     
 * 13:    return output;  
 * 14:   } 
 * With this snippets you can convert if you don’t know the encoding of a certain s
*/
// </reference>
// <author>Dylan</author>
//-----------------------------------------------------------------------
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using SDFL.Core;

namespace ImageLoader
{
    public partial class ImageLoaderFrm : Form
    {
        public ImageLoaderFrm()
        {
            InitializeComponent();
        }


        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDig = new OpenFileDialog();

            string fileName = string.Empty;

            if (ofDig.ShowDialog() == DialogResult.OK)
            {
                fileName = ofDig.FileName;
            }

            // Read the image file into the a filestream, then into a byte array
            if (!string.IsNullOrEmpty(fileName))
            {
                FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);

                byte[] byteBLOBData = new byte[fs.Length];
                               
                fs.Read(byteBLOBData, 0, System.Convert.ToInt32(fs.Length));
                 
                // convert image byte array to Base64String
                richTextBoxBase64Encoding.Text = Convert.ToBase64String(byteBLOBData);
                
                // convert image byte array to HexString (255 to "FF")
                richTextBoxHexString.Text = BitConverter.ToString(byteBLOBData).Replace("-", "");                 
            }         

            btnShowPic.Enabled = true; ;
        }      


        private void btnShowPic_Click(object sender, EventArgs e)
        {
            // convert Base64String back to image byte array
            byte[] temByte = Convert.FromBase64String(richTextBoxBase64Encoding.Text);

            // convert HexString back to image byte array ("FF" to 255)
            string str = richTextBoxHexString.Text;
            byte[] temByte2 = str.ToByteArray();

            // Read the image byte array to MemoryStream
            //MemoryStream stmBLOBData = new MemoryStream(temByte); // this also work
            MemoryStream stmBLOBData = new MemoryStream(temByte2);

            // Image.FromStream(byte array)
            picbox.Image = Image.FromStream(stmBLOBData);

            ((Button)sender).Enabled = false;
        }
    }
}
